package com.mmall.concurrence.deadLock;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class DeadLock implements Runnable {

    public int flag = 1;

    //静态对象是类的所有对象共享的
    private static Object o1 = new Object(), o2 = new Object();

    @Override
    public void run() {
        log.info("flag:{}", flag);
        if (flag == 1) {
            synchronized (o1) {

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2) {
                    log.info("1");
                }

            }
        }

        if (flag == 0) {
            synchronized (o2) {

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1) {
                    log.info("0");
                }

            }
        }

    }


    public static void main(String[] args) {

        DeadLock d1 = new DeadLock(), d2 = new DeadLock();
        d1.flag = 0;
        d2.flag = 1;

        new Thread(d1).start();
        new Thread(d2).start();

    }

}
